const path = require('path')
const moment = require('moment');
const db = require('../db/index')
// 发布新文章的处理函数
exports.addArticle = (req, res) => {

    if (!req.file || req.file.fieldname != 'cover_img') return res.cc('文章封面是必选参数！')
    let articleInfo = {
        ...req.body,
        //文章分类的存放路径
        cover_img: path.join('/uploads', req.file.filename),
        pub_date: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
        //用户id
        author_id: req.user.id,
    }
    let sql = 'insert into ev_articles set ?';
    db.query(sql, articleInfo, (err, data) => {
        if (err) return res.cc(err);
        if (data.affectedRows !== 1) return res.cc('发布文章失败！');
        res.send({
            status: 0,
            message: '发布文章成功！'
        })
    })

}